package cn.itsource.service.impl;

import cn.itsource.annotation.Logs;
import cn.itsource.domain.ArticleType;
import cn.itsource.mapper.ArticleTypeMapper;
import cn.itsource.query.ArticleTypeQuery;
import cn.itsource.service.IArticleTypeService;
import cn.itsource.util.R;
import cn.itsource.vo.ArticleCategory;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

@Service
@Transactional(readOnly = true, propagation = Propagation.SUPPORTS)
public class ArticleTypeServiceImpl implements IArticleTypeService {
    @Autowired
    private ArticleTypeMapper articleTypeMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Logs
    @Transactional
    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void add(ArticleType articleType) {
        articleTypeMapper.add(articleType);
//        redisTemplate.delete("typeTree");
    }
    @Logs
    @Transactional
    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void update(ArticleType articleType) {
        articleType.setUpdateTime(new Date());
        articleTypeMapper.update(articleType);
//        redisTemplate.delete("typeTree");
    }
    @Logs
    @Transactional
    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void del(Long id) {
        articleTypeMapper.del(id);
//        redisTemplate.delete("typeTree");
    }
    @Logs
    @Transactional
    @Override
    @CacheEvict(cacheNames = "articleType", key = "'typeTree'")
    public void delList(List<Long> ids) {
        articleTypeMapper.delList(ids);
//        redisTemplate.delete("typeTree");
    }

    @Override
    public ArticleType get(Long id) {
        return articleTypeMapper.get(id);
    }

    @Override
    @Cacheable(cacheNames = "articleType", key = "'typeTree'")
    public List<ArticleType> getAll() {
        //将所有对象数据从数据库中 存入list中
        List<ArticleType> allTypes = articleTypeMapper.getAll();
        //充当缓存
        Map<Long,ArticleType> map = new HashMap<>();
        for (ArticleType allType : allTypes) {
            map.put(allType.getId(),allType);
        }
//        //list转为Map集合
//        Map<Long,ArticleType> map = allTypes.stream().collect(Collectors.toMap(ArticleType::getId,articleType->articleType));

        //空集合存储（一级）父级的类型
        ArrayList<ArticleType> oneLevelTypes = new ArrayList<>();
        for (ArticleType allType : allTypes) {
            //如果ParentId为空那么就是一级类型
            if (allType.getParentId() == null){
                //向集合添加一级类型
                oneLevelTypes.add(allType);
            }else { //只要是走else 那么就是二级三级四级.....
                //获取父级id
                Long parentId = allType.getParentId();
                //根据父级id拿到父级对象
                ArticleType parentArticleType = map.get(parentId);
                if (parentArticleType != null){
                    parentArticleType.getChildren().add(allType);
                }
            }
        }
        return oneLevelTypes;
    }

    @Override

    public PageInfo<ArticleType> pageQuery(ArticleTypeQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<ArticleType> articleTypes = articleTypeMapper.pageQuery(query);
        redisTemplate.delete("typeTree");
        return new PageInfo<>(articleTypes);
    }
    //查询文章类型和文章数量
    @Override
    public List<ArticleCategory> articleCategory() {
        return articleTypeMapper.articleCategory();
    }

    @Override
    public List<ArticleCategory>pigeonholeCount() {
        return articleTypeMapper.pigeonholeCount();
    }
}
